from libs.ZButton import ZButton
from libs.ZDirector import ZDirector
from libs.ZImage import ZImage
from libs.ZJson import ZJson
from libs.ZLayer import ZLayer
from libs.ZScene import ZScene
from libs.ZText import ZText
from libs.fun import play_music
from project.res.glo import glo , players
from project.res.lang import lang_cn
from project.res.src import src


class PlayerSelectScene ( ZScene ) :
    def __init__ ( self ) :
        super ( PlayerSelectScene , self ).__init__ ( )
        self.background ( )
        self.layer ( )
        self.msg_layer ( )

    def background ( self ) :
        self.add ( ZImage ( self , src.image.gamescene [ 'bg' ] ) )

    def layer ( self ) :
        self.loadData ( )
        self.show_buttons ( )

    def show_buttons ( self ) :
        self.add ( ButtonLayer ( self , self.data ) )
        self.add ( StartBtn ( self ) )

    def loadData ( self ) :
        from os import walk
        arr = [ ]
        file = "data/player/"
        self.data = { }
        for x in walk ( file ) :
            dirs = x [ 2 ]
        for x in dirs :
            if x.split ( "." ) [ 1 ] == "json" :
                arr.append ( x )

        for x in arr :
            name = x.split ( "." ) [ 0 ]
            self.data [ name ] = ZJson ( file + x ).get ( )


class ButtonLayer ( ZLayer ) :
    def __init__ ( self , father , data ) :
        super ( ButtonLayer , self ).__init__ ( father )
        self._pos = 100 , 100
        self.data = data
        self.showbuttons ( )

    def showbuttons ( self ) :
        for id , x in enumerate ( self.data.items ( ) ) :
            logo = ProBtn ( self , x )
            text = ZText ( self , src.font.default , x [ 1 ] [ 'name' ] , 14 , glo.color [ 1 ] )
            logo._pos = 10 + id * 110 , 20
            text._pos = 20 + id * 110 , 120
            self.add ( logo )
            self.add ( text )


class ProBtn ( ZButton ) :
    def __init__ ( self , father , data ) :
        super ( ProBtn , self ).__init__ ( father , data [ 1 ] [ 'large_logo' ] , data [ 1 ] [ 'large_logo' ] )
        self.data = data
        self.name = data [ 1 ] [ 'name' ]

    def action ( self ) :
        self.draw_bg ( )
        players.clear ( )
        players.add ( self.data )

    def draw_bg ( self ) :
        for x in self.father.children :
            if isinstance ( x , ZButton ) :
                x.respose = True
            if isinstance ( x , ZImage ) :
                self.father.remove ( x )
        from pygame import draw
        from pygame import Surface
        surface = Surface ( self._size )
        draw.rect ( surface , glo.color [ 1 ] , [ [ 0 , 0 ] , self._size ] , 0 )
        surface.set_alpha ( 120 )
        self.respose = False
        image = ZImage ( self.father , surface )
        image._pos = self._pos [ 0 ] - 100 , self._pos [ 1 ] - 100
        self.father.add ( image )


class StartBtn ( ZButton ) :
    def __init__ ( self , father ) :
        text1 = ZText ( self , src.font.default , lang_cn.button [ 'start' ] , glo.font [ 1 ] , glo.color [ 0 ] )
        text2 = ZText ( self , src.font.default , lang_cn.button [ 'start' ] , glo.font [ 1 ] , glo.color [ 1 ] )
        super ( StartBtn , self ).__init__ ( father , text1.image , text2.image )
        from config import win_size
        self._pos = win_size [ 0 ] - self._size [ 0 ] - 60 , win_size [ 1 ] - self._size [ 1 ] - 30

    def action ( self ) :
        if not players.players :
            text = ZText ( self.father.msg_info , src.font.default , lang_cn.msg [ 'selectplayer' ] , glo.font [ 1 ] ,
                           glo.color [ 0 ] )
            self.father.msg_info.add ( text )
        else :
            from project.scenes.GameScene.GameScene import GameScene
            ZDirector ( ).show ( GameScene ( ) )
